package icom.acm.action;
/**
 * @author shench
 * 用户激活动作
 */
import icom.acm.bean.T_USER;
import icom.acm.bean.T_USERACTIVATE;
import icom.acm.manager.T_UserActivateMgr;
import icom.acm.manager.T_UserMgr;
import icom.acm.net.EmailThread;
import icom.acm.util.DBConnect;


public class UserActivateAction extends Action /*implements Runnable*/ {
	private String email;
	private String url;
	private String token;
	private T_USERACTIVATE ua;
	
	public String reSend(){
		// 防止重复提交
		String token = (String)session.getAttribute("token");
		if (token==null){
			return "ERROR";
		} else if (!token.equals(this.token)){
			return "ERROR";
		}
		session.removeAttribute("token");
		
		DBConnect db = new DBConnect();
		T_UserMgr um = new T_UserMgr();
		T_USER u = new T_USER();
		u.setU_EMAIL(email);		
		u = um.selectByEmail0(u, db);
		
		if (u==null) return "ACTIVATEERROR";
		
		ua = new T_USERACTIVATE();
		ua.setUA_U_ID(u.getU_ID());
		//ua.setUA_KEY(java.util.UUID.randomUUID().toString());
		
		T_UserActivateMgr uam = new T_UserActivateMgr();
		T_USERACTIVATE tua = uam.selectById0(ua, db);	// 得到ua
		if (tua==null){
			ua.setUA_KEY(java.util.UUID.randomUUID().toString());			
			uam.insert0(ua, db);
		} else {
			ua = tua;
		}
		
		String s = "http://localhost:8080/acmwb/UserActivate_activate?" +
		"ua.UA_U_ID=" + ua.getUA_U_ID() +
		"&ua.UA_KEY=" + ua.getUA_KEY();
		EmailThread et = new EmailThread("注册成功", email, "成功注册Coding", 
				"注册Coding成功,请点下面链接进行激活，如果不能点击，复制链接浏览即可:\n"+
				"<a href='" + s + "'>" + s + "</a><br/>"+
				"<font color='red' size='6'>请在24小时内激活!</font>");
		et.start();
		
		return "HTTPURL";
	}
	
	/**
	 * 通过线程发送邮件
	 * @return
	 */
	public String sendMail(){
		// 防止重复提交
		String token = (String)session.getAttribute("token");
		if (token==null){
			return "ERROR";
		} else if (!token.equals(this.token)){
			return "ERROR";
		}
		session.removeAttribute("token");
		
		ua.setUA_KEY(java.util.UUID.randomUUID().toString());
		
		T_UserActivateMgr uam = new T_UserActivateMgr();
		uam.insert(ua);
		String s = "http://localhost:8080/acmwb/UserActivate_activate?" +
		"ua.UA_U_ID=" + ua.getUA_U_ID() +
		"&ua.UA_KEY=" + ua.getUA_KEY();
		EmailThread et = new EmailThread("注册成功", email, "成功注册Coding", 
				"注册Coding成功,请点下面链接进行激活，如果不能点击，复制链接浏览即可:\n"+
				"<a href='" + s + "'>" + s + "</a><br/>"+
				"<font color='red' size='6'>请在24小时内激活!</font>");
		et.start();
		
		return "HTTPURL";
	}
	/**
	 * 激活用户，当返回false激活失败原因：激活码错误，用户不存在，激活过期
	 * @return
	 */
	public String activate(){
		boolean flag = false;
		DBConnect db = new DBConnect();
		T_UserActivateMgr uam = new T_UserActivateMgr();
		T_UserMgr um = new T_UserMgr();
		
		T_USERACTIVATE ua = uam.selectByIdAndKeyAndDate0(this.ua, db);	// 得到用户激活信息
		int canActivate = 0;
		if (ua!=null){
			canActivate = 2;	// 表示可以激活
		} else {	
			ua = uam.selectByIdAndKey0(this.ua, db);
			if (ua!=null){
				canActivate = 1;	// 表示过期
			}
		}
		if (canActivate>0){
			uam.delete0(ua, db);	// 从列表中删除
			T_USER user = new T_USER();
			user.setU_ID(ua.getUA_U_ID());		
			user = um.selectById0(user, db);		// 得到用户
			session.setAttribute("user", user);
			flag = true;
			if (canActivate>1){		// 可以激活								
				if (user!=null){
					if (user.getU_ROLE()<0){
						user.setU_ROLE(0);
						um.updateRoleById0(user, db);				// 更新用户角色						
					}
				}
			}			
		}
		if (!db.isClose())
			db.close();
		if (flag)return "SUCCESS";
		
		return "ACTIVATEERROR";
	}
	
	
/*	@Override
	public void run() {
		ua.setUA_KEY(java.util.UUID.randomUUID().toString());
		
		T_UserActivateMgr uam = new T_UserActivateMgr();
		uam.insert(ua);
		
		String s = "http://localhost:8080/acmwb/UserActivate_activate?" +
		"ua.UA_U_ID=" + ua.getUA_U_ID() +
		"&ua.UA_KEY=" + ua.getUA_KEY();
		
		
		SendMail sm = new SendMail("注册成功", email, "成功注册Coding", 
				"注册Coding成功,请点下面链接进行激活，如果不能点击，复制链接浏览即可:\n"+
				"<a href='" + s + "'>" + s + "</a><br/>"+
				"<font color='red' size='6'>请在24小时内激活!</font>");
		sm.send();		
	}*/

	
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public T_USERACTIVATE getUa() {
		return ua;
	}

	public void setUa(T_USERACTIVATE ua) {
		this.ua = ua;
	}
}
